package com.tjec.project.businessStandardLibrary.service.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.tjec.common.constant.AttachmentConstants;
import com.tjec.common.enums.CodeNum;
import com.tjec.common.enums.NumCode;
import com.tjec.common.utils.DateUtils;
import com.tjec.common.utils.ServletUtils;
import com.tjec.common.utils.StringUtils;
import com.tjec.framework.security.service.TokenService;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.project.system.domain.BmsAttachment;
import com.tjec.project.system.domain.BmsMdBusinessCategory;
import com.tjec.project.system.mapper.BmsAttachmentMapper;
import com.tjec.project.system.mapper.BmsMdBusinessCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tjec.project.businessStandardLibrary.mapper.BmsKnowledgeBusinessGuideMapper;
import com.tjec.project.businessStandardLibrary.domain.BmsKnowledgeBusinessGuide;
import com.tjec.project.businessStandardLibrary.service.IBmsKnowledgeBusinessGuideService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 业务指南Service业务层处理
 * 
 * @author pangyongfeng
 * @date 2020-04-20
 */
@Service
public class BmsKnowledgeBusinessGuideServiceImpl implements IBmsKnowledgeBusinessGuideService 
{
    @Autowired
    private BmsKnowledgeBusinessGuideMapper bmsKnowledgeBusinessGuideMapper;
    @Autowired
    private BmsMdBusinessCategoryMapper bmsMdBusinessCategoryMapper;
    @Autowired
    private BmsAttachmentMapper bmsAttachmentMapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询业务指南
     * 
     * @param guid 业务指南ID
     * @return 业务指南
     */
    @Override
    public BmsKnowledgeBusinessGuide selectBmsKnowledgeBusinessGuideByGuid(String guid)
    {
        return bmsKnowledgeBusinessGuideMapper.selectBmsKnowledgeBusinessGuideByGuid(guid);
    }

    /**
     * 查询业务指南列表
     * 
     * @param bmsKnowledgeBusinessGuide 业务指南
     * @return 业务指南
     */
    @Override
    public List<BmsKnowledgeBusinessGuide> selectBmsKnowledgeBusinessGuideList(BmsKnowledgeBusinessGuide bmsKnowledgeBusinessGuide)
    {
    	List<BmsKnowledgeBusinessGuide> lists = bmsKnowledgeBusinessGuideMapper.selectBmsKnowledgeBusinessGuideList(bmsKnowledgeBusinessGuide);
    	for(BmsKnowledgeBusinessGuide list : lists) {
    		list.setContentSize(getFileSize(list.getContentLength()));
    	}
        return lists;
    }
    
    public String getFileSize(long fileLength) {
    	DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        String wrongSize = "0B";
        if (fileLength == 0) {
            return wrongSize;
        }
        if (fileLength < 1024) {
            fileSizeString = df.format((double) fileLength) + "B";
        } else if (fileLength < 1048576) {
            fileSizeString = df.format((double) fileLength / 1024) + "KB";
        } else if (fileLength < 1073741824) {
            fileSizeString = df.format((double) fileLength / 1048576) + "MB";
        } else {
            fileSizeString = df.format((double) fileLength / 1073741824) + "GB";
        }
        return fileSizeString;
    }

    /**
     * 新增业务指南
     * 
     * @param bmsKnowledgeBusinessGuide 业务指南
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertBmsKnowledgeBusinessGuide(BmsKnowledgeBusinessGuide bmsKnowledgeBusinessGuide)
    {   System.out.println(bmsKnowledgeBusinessGuide.toString());
        bmsKnowledgeBusinessGuide.setCreateTime(DateUtils.getNowDate());
        bmsKnowledgeBusinessGuide.setUpdateTime(DateUtils.getNowDate());
        //附件guid
        String contentGuid = bmsKnowledgeBusinessGuide.getContentGuid();
        String guid = UUID.randomUUID().toString();
        bmsKnowledgeBusinessGuide.setGuid(guid);
        int res=bmsKnowledgeBusinessGuideMapper.insertBmsKnowledgeBusinessGuide(bmsKnowledgeBusinessGuide);
        BmsAttachment bmsAttachment= new BmsAttachment();
        bmsAttachment.setBizId(guid);
        bmsAttachment.setGuid(contentGuid);
        bmsAttachment.setCategory(AttachmentConstants.KNOWLEDGE_BUSINESS_GUIDE);
        bmsAttachmentMapper.updateBmsAttachment(bmsAttachment);
        return res;
    }

    /**
     * 修改业务指南
     * 
     * @param bmsKnowledgeBusinessGuide 业务指南
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateBmsKnowledgeBusinessGuide(BmsKnowledgeBusinessGuide bmsKnowledgeBusinessGuide)
    {
        bmsKnowledgeBusinessGuide.setUpdateTime(DateUtils.getNowDate());
        String contentGuid = bmsKnowledgeBusinessGuide.getContentGuid();
        //先把上一次的附件去掉
        String guid=bmsKnowledgeBusinessGuide.getGuid();
        bmsAttachmentMapper.updateBmsAttachmentByGuid(guid, tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
        //更新新的附件状态
        BmsAttachment bmsAttachment= new BmsAttachment();
        bmsAttachment.setBizId(guid);
        bmsAttachment.setDelFlag(NumCode.ZERO.getCode());
        bmsAttachment.setGuid(contentGuid);
        bmsAttachment.setCategory(AttachmentConstants.KNOWLEDGE_BUSINESS_GUIDE);
        bmsAttachmentMapper.updateBmsAttachment(bmsAttachment);
        int res= bmsKnowledgeBusinessGuideMapper.updateBmsKnowledgeBusinessGuide(bmsKnowledgeBusinessGuide);
        return res;
    }

    /**
     * 批量删除业务指南
     * 
     * @param guids 需要删除的业务指南ID
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteBmsKnowledgeBusinessGuideByGuids(String[] guids)
    {
        int res= bmsKnowledgeBusinessGuideMapper.deleteBmsKnowledgeBusinessGuideByGuids(guids);
        //此处改为逻辑删除
        bmsAttachmentMapper.updateBmsAttachmentByBizId(guids, tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
        return res;
    }

    /**
     * 删除业务指南信息
     * 
     * @param guid 业务指南ID
     * @return 结果
     */
    @Override
    public int deleteBmsKnowledgeBusinessGuideByGuid(String guid)
    {
        return bmsKnowledgeBusinessGuideMapper.deleteBmsKnowledgeBusinessGuideByGuid(guid);
    }
    /**
     *
     *@describe 查询业务指南  详情
     *@params [bmsKnowledgeBusinessGuide]
     *@return java.util.List<com.tjec.project.businessStandardLibrary.domain.BmsKnowledgeBusinessGuide>
     *@author VillWang
     *@date 2020/4/27
     *@other
     *
     */
    @Override
    public BmsKnowledgeBusinessGuide selectBmsKnowledgeBusinessGuideselectListDetails(String guid) {

            return bmsKnowledgeBusinessGuideMapper.selectBmsKnowledgeBusinessGuideselectListDetails(guid);

    }

    /**
     *
     *@describe 查询业务指南  带上业务类别的类别树
     *@params [bmsKnowledgeBusinessGuide]
     *@return java.util.List<com.tjec.project.businessStandardLibrary.domain.BmsKnowledgeBusinessGuide>
     *@author VillWang
     *@date 2020/4/27
     *@other
     *
     */
    @Override
    public List<BmsKnowledgeBusinessGuide> selectBmsKnowledgeBusinessGuideselectList(BmsKnowledgeBusinessGuide bmsKnowledgeBusinessGuide) {
        String businessCategoryId=bmsKnowledgeBusinessGuide.getBusinessCategoryId();
        List<String> ids=new ArrayList<>();
        List<BmsKnowledgeBusinessGuide> bms=new ArrayList<>();
        //业务树查询
        if(StringUtils.isNotBlank(businessCategoryId)){
              //获取业务层级
            int level = businessLevel(businessCategoryId);

              if(level==CodeNum.ONE.getCode()){
                  //第一层
                 ids= bmsMdBusinessCategoryMapper.selectBmsMdBusinessCategoryChildIdsOne(businessCategoryId);
              }else if(level==CodeNum.TWO.getCode()){
                  //第二层
                 ids= bmsMdBusinessCategoryMapper.selectBmsMdBusinessCategoryChildIdsTwo(businessCategoryId);
              }else {
                  //第三层
                  ids.add(businessCategoryId);
              }
              //没有第三层的问题
              if(ids.isEmpty()){
                  ids.add(businessCategoryId);
              }
               bmsKnowledgeBusinessGuide.setGuIds(ids);
               bms=bmsKnowledgeBusinessGuideMapper.selectBmsKnowledgeBusinessListByGuIds(bmsKnowledgeBusinessGuide);
        }else {
               //不点业务树查询
               bms= bmsKnowledgeBusinessGuideMapper.selectBmsKnowledgeBusinessListByGuIds(bmsKnowledgeBusinessGuide);

        }
        return bms;
    }



    /**
     *
     *@describe   获取业务类别的层级
     *@params [businessCategoryId]
     *@return java.lang.String
     *@author VillWang
     *@date 2020/4/27
     *@other
     *
     */
    @Override
    public int businessLevel(String businessCategoryId) {

        BmsMdBusinessCategory bms=  bmsMdBusinessCategoryMapper.selectBmsMdBusinessCategoryByGuId(businessCategoryId);
        String parentId= bms.getParentId();
        //获取下级定位是否只有2层的问题
        BmsMdBusinessCategory bmsMdBusinessCategorynew = new BmsMdBusinessCategory();
        bmsMdBusinessCategorynew.setParentId(businessCategoryId);
        List<BmsMdBusinessCategory> bmsCategory= bmsMdBusinessCategoryMapper.selectBmsMdBusinessCategoryList(bmsMdBusinessCategorynew);
        int flag=0;
        if(bmsCategory==null||bmsCategory.isEmpty()||bmsCategory.equals("")){
            //处理没有二级  和三级的  一级菜单
            return CodeNum.THREE.getCode();
        }else {
            for (BmsMdBusinessCategory bms2 : bmsCategory) {

                bmsMdBusinessCategorynew.setParentId(bms2.getGuid());
                List<BmsMdBusinessCategory> bmsCategory3= bmsMdBusinessCategoryMapper.selectBmsMdBusinessCategoryList(bmsMdBusinessCategorynew);
                for (BmsMdBusinessCategory bms3:bmsCategory3) {
                    if(bms3==null||bms3.equals("")){
                    }else {
                        //标记是否有第三级
                        flag=1;
                    }
                }

            }
        }
        if(flag==0){
            //标记是否有第三级
            return CodeNum.TWO.getCode();
        }
        //第一层
        if(parentId.equals(NumCode.ZERO.getCode())){
            return CodeNum.ONE.getCode();
        }else {
            //第二层
            BmsMdBusinessCategory bms2=  bmsMdBusinessCategoryMapper.selectBmsMdBusinessCategoryByGuId(bms.getParentId());
            String parentId2= bms2.getParentId();
            if(parentId2.equals(NumCode.ZERO.getCode())){
                return CodeNum.TWO.getCode();
            }else {
                //第三层
                return CodeNum.THREE.getCode();
            }
        }

    }

}
